package com.liyong.bos.service;

import com.liyong.bos.dao.TUserMapper;
import com.liyong.bos.domain.TUser;
import com.liyong.bos.domain.TUserExample;
import com.liyong.bos.utils.MD5Util;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by ApacheMoy on 2017/4/5.
 */
@Service
public class StaffServiceImpl implements StaffService{
    @Autowired
    private TUserMapper tUserMapper;
/**
   * 用户登陆验证
    * */
    public TUser finUser(String username, String password){
        TUserExample tUserExample=new TUserExample();
        tUserExample.createCriteria().andUsernameEqualTo(username).andPasswordEqualTo(MD5Util.getMD5Str(password));
        List<TUser> list=tUserMapper.selectByExample(tUserExample);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

/**
    *用户注册
    * */
    public int insertUser(TUser tUser){
        tUser.setPassword(MD5Util.getMD5Str(tUser.getPassword()));
        return tUserMapper.insertSelective(tUser);
    }

  /**
    * 查询全部用户（分页按条件）
    * */
    public List<TUser> finAllUser(String userNameSearch, int start, int pageSize){
        TUserExample tUserExample=new TUserExample();
        tUserExample.setLimitStart(start);
        tUserExample.setLimitEnd(pageSize);
        if(StringUtils.isNotBlank(userNameSearch)){
            tUserExample.createCriteria().andUsernameLike(userNameSearch);
        }
        List<TUser> users=tUserMapper.selectByExample(tUserExample);
        if(users.size()>0){
            return users;
        }else
        return null;
    }

    /**
     * 更新用户信息，无设置信息则不更新
     * */
    public int updateUser(TUser tUser) {
        int isUpdate=0;
        if(StringUtils.isNotBlank(tUser.getId())) {
            isUpdate = tUserMapper.updateByPrimaryKey(tUser);
        }
        return isUpdate;
    }

    /**
     * 删除用户
     * */
    public int deleteUser(String id){
        int isDelete=0;
        if(StringUtils.isNotBlank(id)) {
            isDelete = tUserMapper.deleteByPrimaryKey(id);
        }
        return isDelete;
    }
}
